package com.xuecheng.content.api;

import com.xuecheng.base.exception.ValidationGroups;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.model.dto.*;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@Api(value = "课程基础信息管理", tags = "课程基础信息管理接口")
public class CourseBaseInfoController {

    @Autowired
    private CourseBaseService courseBaseService;

    @ApiOperation("课程查询接口")
    @PostMapping("/course/list")
    public PageResult<CourseBaseInfoDto> list(PageParams pageParams, @RequestBody(required = false) QueryCourseParamsDto queryCourseParamsDto) {
//        log.info("查询参数：pageParams={}, queryParams={}", pageParams, queryCourseParamsDto);
        queryCourseParamsDto.setCompanyId(Long.parseLong(SecurityUtil.getUser().getCompanyId()));   //自动获取机构id

        return courseBaseService.list(pageParams, queryCourseParamsDto);
    }

    @ApiOperation("新增课程基础信息")
    @PostMapping("/course")
    public CourseBaseInfoDto createCourseBase(@RequestBody @Validated(ValidationGroups.Insert.class) AddCourseDto addCourseDto) {
        Long companyId = 1232141425L;  //传递企业信息
        //传递教师信息
        CourseTeacherDto courseTeacher = new CourseTeacherDto();
        courseTeacher.setTeacherName("禾教师");
        courseTeacher.setPosition("金牌教师");
        courseTeacher.setIntroduction("从事互联网开发十年,拥有更丰富的java开发经验");

        return courseBaseService.createCourseBase(companyId, courseTeacher, addCourseDto);
    }

    @ApiOperation("修改课程信息")
    @PutMapping("/course")
    public CourseBaseInfoDto modifyCourseBase(@RequestBody @Validated(ValidationGroups.Update.class) EditCourseDto editCourseDto) {
        Long companyId = 1232141425L;  //传递企业信息
        CourseBaseInfoDto courseBaseInfoDto = courseBaseService.updateCourseDase(companyId, editCourseDto);

        return courseBaseInfoDto;
    }

    @ApiOperation("据课程id查询课程")
    @GetMapping("/course/{courseId}")
    public CourseBaseInfoDto getCourseBaseInfo(@PathVariable Long courseId) {
        return courseBaseService.selectCourseBaseAndMarketById(courseId);
    }

    @ApiOperation("删除课程基础信息")
    @DeleteMapping("/course/{courseId}")
    public void deleteCourseBase(@PathVariable Long courseId){
        courseBaseService.deleteCourseBase(courseId);
    }

//    media 服务中更新图片的远程接口
    @ApiOperation("课程基本信息更新图片")
    @PostMapping("/course/{courseId}/picture")
    public Boolean updateCoursePicture( @PathVariable Long courseId,  @RequestParam String picUrl) {
        return courseBaseService.updateCoursePic(courseId, picUrl);
    }

    @ApiOperation("课程计划id提取图片md5值")
    @PostMapping("/course/courseIdExtractMd5/{courseId}")
    public Map<String,String> courseIdExtractMd5(@PathVariable String courseId){
        return courseBaseService.courseIdExtractMd5(courseId);
    }




}
